package com.shwread.android.ui.page;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw.activity.BookSearchActivity;
import com.shwread.android.qysw.activity.BookShelfActivity;
import com.shwread.android.qysw.activity.ColumnGameMoreActivity;
import com.shwread.android.qysw.activity.GameWebActivity;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000182.R;
import com.shwread.android.ui.customview.BannerOnClickListener;
import com.shwread.android.ui.customview.MyListView;
import com.shwread.android.ui.customview.QYViewPager;
import com.shwread.android.ui.customview.ViewPagerAdapter;
import com.shwread.android.ui.dialog.GuideDialog;
import com.shwread.android.ui.dialog.NoNetDialog;
import com.shwread.android.ui.widget.BookCoverView;
import com.shwread.android.ui.widget.TeamInfoGridView;
import com.shwread.http.base.Log;
import com.shwread.http.base.ObjectHttpResponseHandler;
import com.shwread.httpsdk.http.callback.BaseAction;
import com.shwread.httpsdk.http.face.QrySpecialInfoListAction;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.apache.http.Header;
import org.geometerplus.android.util.QyreaderPerferencesUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import base.tina.core.task.infc.ITaskResult;
import logic.bean.BannerInfo;
import logic.bean.BookColumnInfos;
import logic.bean.BookInfo;
import logic.bean.ColumnInfos;
import logic.bean.GameColumnInfos;
import logic.bean.GameInfos;
import logic.constants.DefaultConsts;
import logic.util.ImageLoaderUtil;
import logic.util.NetworkUtil;
import logic.util.Utils;


/**
 * 精品页面
 */
@SuppressLint("NewApi")
public class BoutiquePage extends AbstractUIPage<MainActivity> implements OnClickListener {
    private QYViewPager vpPager;
    private ViewGroup vgPoints;
    private ViewPagerAdapter pagerAdapter;
    private static Rect ViewPagerRect;
    private HashMap<Integer, View> views;
    private List<BannerInfo> banners;

    private TextView tvTitle, tvJX, tvFL, tvYX;
    private ImageView ivPersion;
    private ImageView btnSearch, btnBookshelf;
    private View loadingView, freshView, headView;
    private ImageView loadingImg/*, ivPerson*/;
    private AnimationDrawable animationDrawable;
    /**
     * 判断最后一次数据加载是否成功，不成功的话每次进入此页面需要显示加载动画
     */
    private boolean isLastSuccess = false;
    private final int TYPE_ONE = 1, TYPE_TWO = 2, TYPE_COUNT = 2; // 适配器加载不同的item

    private static BoutiquePage instance;
    private ListView lv;
    private ListView lvFL;
    private ListView lvGame;
    private List<ColumnInfos> data;
    private Adapter adapter;
    private FLAdapter flAdapter;
    private GameAdapter gameAdapter;
    private BookListHandler handler;
    public static boolean fromBoutique = false;

    public static BoutiquePage getInstance(MainActivity t) {
        if (instance == null) {
            instance = new BoutiquePage(t);
        } else {
            instance.setActivity(t);
        }
        return instance;
    }

    private BoutiquePage(MainActivity t) {
        super(t);
    }

    public static void destroy() {
        if (instance != null) {
            instance.dispose();
            instance = null;
        }
    }

    @Override
    public <E extends Activity> View createView(E arg0, int arg1) {
        if (null != curMyView) return curMyView;
        curMyView = bActivity.getLayoutInflater().inflate(R.layout.boutique_lay, null);
        initView();
        setListener();
        initGuide();
        return curMyView;
    }

    // 大的适配器
    private class Adapter extends BaseAdapter {
        ViewHolder holder;

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            final ColumnInfos columnInfos = data.get(position);
            List<BookInfo> currData = columnInfos.getBookInfos();
            //			System.out.println("精品页面  -->  列名：" + columnInfos.getColumnName() + ",总书籍：" + currData.size() + "本," + currData.toString());
            if (view == null) {
                holder = new ViewHolder();
                view = LayoutInflater.from(bActivity).inflate(R.layout.boutique_item, null);
                holder.title = (TextView) view.findViewById(R.id.boutique_item_title);
                holder.subLv = (MyListView) view.findViewById(R.id.boutique_item_listview);
                holder.more = view.findViewById(R.id.boutique_item_more_layout);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            // holder.more.setVisibility((currData != null && currData.size() >
            // 4) ? View.VISIBLE : View.GONE);是否显示更多
            holder.title.setText(columnInfos.getColumnName());
            holder.more.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Bundle b = new Bundle();
                    b.putString("columnName", columnInfos.getColumnName());
                    b.putInt("columnId", columnInfos.getColumnId());
                    b.putInt("mark", 1);
                    bActivity.goColumnDetailActivity(b);
                }
            });
            SubAdapter subAdapter = new SubAdapter();
            subAdapter.setData(columnInfos.getLayout(), columnInfos.getPhoneIndexShowNum(), currData);
            holder.subLv.setAdapter(subAdapter);
            holder.subLv.setFocusable(false);
            holder.subLv.setFocusableInTouchMode(false);
            // holder.subLv.setOnItemClickListener(new
            // SubListViewItemClick(position));
            return view;
        }
    }

    private class SubListViewItemClick implements OnItemClickListener {
        private int position;

        public SubListViewItemClick(int postion) {
            this.position = postion;
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
            if (position % 2 == 1) {
                final BookInfo bean = (BookInfo) arg0.getAdapter().getItem(arg2);
                bActivity.goBookDetailActivity(bean);
            }
        }
    }

    private class ViewHolder {
        TextView title, name, author, intro;
        ImageView typeImg;
        BookCoverView cover;
        // SmartImageView cover;
        MyListView subLv;
        View bookView, more;
    }

    private class SubViewHolder0 {
        TextView author1, author2, author3, intro1, intro2, intro3;
        BookCoverView bookCoverView1, bookCoverView2, bookCoverView3; // 显示封面
        ImageView musicImg1, musicImg2, musicImg3;               // 显示音乐的图标
        LinearLayout boutique_item_one_layout, boutique_item_two_layout, boutique_item_three_layout;
    }

    private class SubViewHolder1 {
        TextView author, name, intro, tv_boutique_tag;
        ImageView musicImg;
        BookCoverView bookCoverView;
        RelativeLayout new_boutique_book_item_layout;
    }

    // 子listview的适配器
    private class SubAdapter extends BaseAdapter implements OnClickListener {
        private List<BookInfo> data;
        SubViewHolder0 holder0;
        SubViewHolder1 holder1;
        private int parentPosition;
        private int pos;
        private int maxlength;
        private int showlength = 0;

        public void setData(int parentPosition, int maxlength, List<BookInfo> data) {
            this.data = data;
            this.parentPosition = parentPosition;
            this.maxlength = maxlength;
        }

        @Override
        public int getCount() {
            int count;
            showlength = maxlength > data.size() ? data.size() : maxlength;
            if (parentPosition == 1) {
                if (showlength % 3 != 0) {
                    count = showlength / 3 + 1;
                } else count = showlength / 3;
            } else {
                count = showlength;
            }
            return count;
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        /** 根据position返回相应的Item */
        // @Override
        // public int getItemViewType(int position) {
        // // TODO Auto-generated method stub
        // if(TYPE_ONE==parentPosition%2){
        // return TYPE_ONE;
        // }else{
        // return TYPE_TWO;
        // }
        // }

        /**
         * 该方法返回多少个不同的布局
         */
        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return TYPE_COUNT;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {// 判定position的位置

            // int type=getItemViewType(position);
            if (convertView == null) {
                switch (parentPosition) {
                    case TYPE_ONE:// 为偶数显示三本
                        holder0 = new SubViewHolder0();
                        convertView = LayoutInflater.from(bActivity).inflate(R.layout.new_boutique_sub_itemthree, null);
                        holder0.author1 = (TextView) convertView.findViewById(R.id.boutique_book_author1);
                        holder0.author2 = (TextView) convertView.findViewById(R.id.boutique_book_author2);
                        holder0.author3 = (TextView) convertView.findViewById(R.id.boutique_book_author3);
                        holder0.intro1 = (TextView) convertView.findViewById(R.id.boutique_book_description1);
                        holder0.intro2 = (TextView) convertView.findViewById(R.id.boutique_book_description2);
                        holder0.intro3 = (TextView) convertView.findViewById(R.id.boutique_book_description3);
                        holder0.musicImg1 = (ImageView) convertView.findViewById(R.id.common_book_item_type_img1);
                        holder0.musicImg2 = (ImageView) convertView.findViewById(R.id.common_book_item_type_img2);
                        holder0.musicImg3 = (ImageView) convertView.findViewById(R.id.common_book_item_type_img3);
                        holder0.bookCoverView1 = (BookCoverView) convertView.findViewById(R.id.common_book_item_cover1);
                        holder0.bookCoverView2 = (BookCoverView) convertView.findViewById(R.id.common_book_item_cover2);
                        holder0.bookCoverView3 = (BookCoverView) convertView.findViewById(R.id.common_book_item_cover3);
                        holder0.boutique_item_one_layout = (LinearLayout) convertView.findViewById(R.id.boutique_item_one_layout);
                        holder0.boutique_item_one_layout.setTag(position);
                        holder0.boutique_item_one_layout.setOnClickListener(this);
                        holder0.boutique_item_two_layout = (LinearLayout) convertView.findViewById(R.id.boutique_item_two_layout);
                        holder0.boutique_item_two_layout.setOnClickListener(this);
                        holder0.boutique_item_two_layout.setTag(position);
                        holder0.boutique_item_three_layout = (LinearLayout) convertView.findViewById(R.id.boutique_item_three_layout);
                        holder0.boutique_item_three_layout.setOnClickListener(this);
                        holder0.boutique_item_three_layout.setTag(position);
                        convertView.setTag(holder0);
                        break;
                    case TYPE_TWO:// 为基数显示一本
                        holder1 = new SubViewHolder1();
                        convertView = LayoutInflater.from(bActivity).inflate(R.layout.new_boutique_sub_itemo, null);
                        holder1.tv_boutique_tag = (TextView) convertView.findViewById(R.id.tv_boutique_tag);
                        holder1.new_boutique_book_item_layout = (RelativeLayout) convertView.findViewById(R.id.new_boutique_book_item_layout_chird);
                        holder1.new_boutique_book_item_layout.setOnClickListener(this);
                        holder1.new_boutique_book_item_layout.setTag(position);
                        holder1.author = (TextView) convertView.findViewById(R.id.common_book_item_author);
                        holder1.name = (TextView) convertView.findViewById(R.id.common_book_item_name);
                        holder1.intro = (TextView) convertView.findViewById(R.id.common_book_item_intro);
                        holder1.musicImg = (ImageView) convertView.findViewById(R.id.common_book_item_type_img);
                        holder1.bookCoverView = (BookCoverView) convertView.findViewById(R.id.common_book_item_cover0);
                        convertView.setTag(holder1);
                        break;
                    default:
                        break;
                }
            } else {
                switch (parentPosition) {
                    case TYPE_ONE:
                        holder0 = (SubViewHolder0) convertView.getTag();
                        break;
                    case TYPE_TWO:
                        holder1 = (SubViewHolder1) convertView.getTag();
                        break;
                    default:
                        break;
                }
            }
            // holder1.tag.setText(b.getContentTagName());
            // holder.name.setText(b.getName());
            // holder.intro.setText(b.getShortIntro());
            switch (parentPosition) {
                case TYPE_ONE:
                    BookInfo b1 = null;
                    if (position * 3 < showlength) b1 = (BookInfo) getItem(position * 3);
                    if (b1 != null) {
                        holder0.boutique_item_one_layout.setVisibility(View.VISIBLE);
                        holder0.bookCoverView1.setImageUrl(b1.getLogoUrl(), b1.getCoverTagPicUrl());
                        holder0.intro1.setText(b1.getName());
                        holder0.author1.setText(b1.getAuthor());
                        setBookTypeIcon(holder0.musicImg1, b1.getContent_type());
                    } else {
                        holder0.boutique_item_one_layout.setVisibility(View.GONE);
                    }
                    BookInfo b2 = null;
                    if (position * 3 + 1 < showlength) {
                        b2 = (BookInfo) getItem(position * 3 + 1);
                    }
                    if (b2 != null) {
                        holder0.boutique_item_two_layout.setVisibility(View.VISIBLE);
                        holder0.bookCoverView2.setImageUrl(b2.getLogoUrl(), b2.getCoverTagPicUrl());
                        holder0.intro2.setText(b2.getName());
                        holder0.author2.setText(b2.getAuthor());
                        setBookTypeIcon(holder0.musicImg2, b1.getContent_type());
                    } else {
                        holder0.boutique_item_two_layout.setVisibility(View.GONE);
                    }
                    BookInfo b3 = null;
                    if (position * 3 + 2 < showlength) {
                        b3 = (BookInfo) getItem(position * 3 + 2);
                    }
                    if (b3 != null) {
                        holder0.boutique_item_three_layout.setVisibility(View.VISIBLE);
                        holder0.bookCoverView3.setImageUrl(b3.getLogoUrl(), b3.getCoverTagPicUrl());
                        holder0.intro3.setText(b3.getName());
                        holder0.author3.setText(b3.getAuthor());
                        setBookTypeIcon(holder0.musicImg3, b3.getContent_type());
                    } else {
                        holder0.boutique_item_three_layout.setVisibility(View.GONE);
                    }
                    break;
                case TYPE_TWO:
                    BookInfo b = (BookInfo) getItem(position);
                    holder1.bookCoverView.setImageUrl(b.getLogoUrl(), b.getCoverTagPicUrl());
                    holder1.name.setText(b.getName());
                    holder1.author.setText("作者：" + b.getAuthor());
                    holder1.intro.setText(b.getShortIntro());
                    //				holder1.tv_boutique_tag.setText(b.getTagName());
                    setBookTypeIcon(holder1.musicImg, b.getContent_type());
                    break;
                default:
                    break;
            }
            return convertView;
        }

        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();
            switch (v.getId()) {
                case R.id.boutique_item_one_layout:
                    BookInfo b1 = (BookInfo) getItem(pos * 3);
                    bActivity.goBookDetailActivity(b1);
                    break;
                case R.id.boutique_item_two_layout:
                    BookInfo b2 = (BookInfo) getItem(pos * 3 + 1);
                    bActivity.goBookDetailActivity(b2);
                    break;
                case R.id.boutique_item_three_layout:
                    BookInfo b3 = (BookInfo) getItem(pos * 3 + 2);
                    bActivity.goBookDetailActivity(b3);
                    break;
                case R.id.new_boutique_book_item_layout_chird:
                    BookInfo b4 = (BookInfo) getItem(pos);
                    bActivity.goBookDetailActivity(b4);
                    break;
                default:
                    break;
            }
        }
    }

    void initView() {
        banners = new ArrayList<>();
        data = new ArrayList<>();
        views = new HashMap<>();
        tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
        tvTitle.setText(bActivity.getResources().getString(R.string.maintab_bookshelf));
        ivPersion = (ImageView) curMyView.findViewById(R.id.common_header_person);
        ivPersion.setVisibility(View.VISIBLE);
        btnBookshelf = (ImageView) curMyView.findViewById(R.id.common_header_bookshelf);
        btnBookshelf.setVisibility(View.VISIBLE);
        btnSearch = (ImageView) curMyView.findViewById(R.id.common_header_search);
        btnSearch.setVisibility(View.VISIBLE);
//		ivPerson = (ImageView) curMyView.findViewById(R.id.common_header_person);
        lv = (ListView) curMyView.findViewById(R.id.boutique_listview);
        lvFL = (ListView) curMyView.findViewById(R.id.boutique_fl_listview);
        lvGame = (ListView) curMyView.findViewById(R.id.boutique_yx_listview);
        loadingView = curMyView.findViewById(R.id.common_loading_layout);
        loadingImg = (ImageView) curMyView.findViewById(R.id.common_loading__img);
        freshView = curMyView.findViewById(R.id.boutipue_view);
        headView = bActivity.getLayoutInflater().inflate(R.layout.boutique_head, null);
        headView.setVisibility(View.GONE);
        vpPager = (QYViewPager) curMyView.findViewById(R.id.boutique_head_pager);
        vgPoints = (ViewGroup) curMyView.findViewById(R.id.boutique_head_points);// banner下面的点
        tvJX = (TextView) curMyView.findViewById(R.id.boutique_head_jx);
        tvFL = (TextView) curMyView.findViewById(R.id.boutique_head_fl);
        tvYX = (TextView) curMyView.findViewById(R.id.boutique_head_yx);
        //默认精选
        clickJX();
        // 获取ViewPager在屏幕的位置
        final ViewTreeObserver observer = vpPager.getViewTreeObserver();
        OnGlobalLayoutListener global = new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (ViewPagerRect == null) {
                    ViewPagerRect = new Rect();
                    vpPager.getGlobalVisibleRect(ViewPagerRect);
                }
            }
        };
        observer.addOnGlobalLayoutListener(global);
        //		lv.addHeaderView(view);
    }

    void setListener() {
        btnBookshelf.setOnClickListener(this);
        btnSearch.setOnClickListener(this);
//		ivPerson.setOnClickListener(this);
        tvJX.setOnClickListener(this);
        tvFL.setOnClickListener(this);
        tvYX.setOnClickListener(this);
        ivPersion.setOnClickListener(this);
        vpPager.setOnPageChangeListener(new PagerChangeListener());

        adapter = new Adapter();
        lv.setAdapter(adapter);// 列表添加适配器
        flAdapter = new FLAdapter();
        lvFL.setAdapter(flAdapter);
        gameAdapter = new GameAdapter();
        lvGame.setAdapter(gameAdapter);
        pagerAdapter = new ViewPagerAdapter(bActivity, views, new BannerClick());
        vpPager.setAdapter(pagerAdapter);// banner
    }

    private QrySpecialInfoListAction bannerAction = null;

    @Override
    public int enter(IUIPage<?> arg0) {
        freshView.scrollTo(0, 0);
        if (!isLastSuccess) {
            if (NetworkUtil.isNetAvailable(bActivity)) {
                updateLoadingView(0);
                if (bannerAction == null) {
                    bannerAction = new QrySpecialInfoListAction(bActivity, new GetBannerAction());
                    bannerAction.start();
                }
                NewActionHelp.qryBooksList(context, 3, new BookListHandler());
            } else {
                new NoNetDialog(bActivity).show();
            }
        }
        showGuide();
        return 0;
    }

    @Override
    public int leave(IUIPage<?> arg0) {
        return 0;
    }

    @Override
    public void notifyView(int cmd, Bundle bundle) {

    }

    @Override
    public void setStatus(int arg0) {

    }

    @Override
    public View updateView(int arg0, ITaskResult arg1) {
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_bookshelf:
                fromBoutique = true;
                bActivity.startActivity(new Intent(bActivity, BookShelfActivity.class));
                break;
            case R.id.common_header_search:
                bActivity.startActivity(new Intent(bActivity, BookSearchActivity.class));
                break;
            case R.id.boutique_head_jx:
                clickJX();
                break;
            case R.id.boutique_head_fl:
                clickFL();
                break;
            case R.id.boutique_head_yx:
                clickYX();
                break;
            case R.id.common_header_person:
                bActivity.goPersonalActivity();
                break;
            default:
                break;
        }
    }

    /**
     * 选中游戏栏
     */
    private void clickYX() {
        ShapeDrawable bg = getModuleItemBgDrawable(0xffff9037, 1);
        tvFL.setBackground(bg);
        tvFL.setTextColor(0xffffffff); //设为白色
        tvFL.setText("分类");
        bg = getModuleItemBgDrawable(0xff71b9e2, 1);
        tvJX.setBackground(bg);
        tvJX.setTextColor(0xffffffff);
        tvJX.setText("精选");
        bg = getModuleItemBgDrawable(0xff46A626, 2);
        tvYX.setBackground(bg);
        tvYX.setTextColor(0xff46A626); //设为下发的颜色
        tvYX.setText(Html.fromHtml("<u>游戏</u>"));
        lv.setVisibility(View.GONE);
        lvFL.setVisibility(View.GONE);
        lvGame.setVisibility(View.VISIBLE);
        lv.setFocusable(false);
        lvFL.setFocusable(false);
        freshView.scrollTo(0, 0);
    }

    /**
     * 选中精选栏
     */
    void clickJX() {
        //71b9e2
        ShapeDrawable bg = getModuleItemBgDrawable(0xffff9037, 1);
        tvFL.setBackground(bg);
        tvFL.setTextColor(0xffffffff); //设为白色
        tvFL.setText("分类");
        bg = getModuleItemBgDrawable(0xff46A626, 1);
        tvYX.setBackground(bg);
        tvYX.setTextColor(0xffffffff);
        tvYX.setText("游戏");
        bg = getModuleItemBgDrawable(0xff71b9e2, 2);
        tvJX.setBackground(bg);
        tvJX.setTextColor(0xff71b9e2); //设为下发的颜色
        tvJX.setText(Html.fromHtml("<u>精选</u>"));
        lv.setVisibility(View.VISIBLE);
        lvFL.setVisibility(View.GONE);
        lvGame.setVisibility(View.GONE);
        lv.setFocusable(false);
        lvGame.setFocusable(false);
        freshView.scrollTo(0, 0);
    }

    /**
     * 选中分类栏
     */
    void clickFL() {
        //ff9037
        ShapeDrawable bg = getModuleItemBgDrawable(0xff71b9e2, 1);
        tvJX.setBackground(bg);
        tvJX.setTextColor(0xffffffff); //设为白色
        tvJX.setText("精选");
        bg = getModuleItemBgDrawable(0xff46A626, 1);
        tvYX.setBackground(bg);
        tvYX.setTextColor(0xffffffff);
        tvYX.setText("游戏");
        bg = getModuleItemBgDrawable(0xffff9037, 2);
        tvFL.setBackground(bg);
        tvFL.setTextColor(0xffff9037); //设为下发的颜色
        tvFL.setText(Html.fromHtml("<u>分类</u>"));
        lv.setVisibility(View.GONE);
        lvFL.setVisibility(View.VISIBLE);
        lvGame.setVisibility(View.GONE);
        lvFL.setFocusable(false);
        lvGame.setFocusable(false);
        freshView.scrollTo(0, 0);
    }

    /**
     * 获取背景drawable
     *
     * @param color
     * @param flag  1为填充背景， 2为描边
     * @return
     */
    private ShapeDrawable getModuleItemBgDrawable(int color, int flag) {
        int radius = 8;
        float[] outerR = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
        RoundRectShape roundRectShape = new RoundRectShape(outerR, null, outerR);
        ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape); // 组合圆角矩形和ShapeDrawable
        shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        shapeDrawable.getPaint().setColor(color); // 设置形状的颜色
        if (flag == 1) {
            shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        } else {
            shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
            shapeDrawable.getPaint().setStrokeWidth(1);
        }
        return shapeDrawable;
    }

    void clickBanner(BannerInfo info) {
        switch (info.getType()) {
            case BannerInfo.TYPE_BANNER:
                break;
            case BannerInfo.TYPE_BOOKS:
                break;
            case BannerInfo.TYPE_BOOKSURL:
                bActivity.goBookDetailActivity(info.getBooks());
                break;
            case BannerInfo.TYPE_URL:
                bActivity.goWebViewActivity(info);
                break;
        }
    }

    private void initBannerView() {
        if (banners.size() == 0) {
            return;
        }
        vgPoints.removeAllViews();
        for (int i = 0; i < banners.size() - 2; i++) {
            View view = bActivity.getLayoutInflater().inflate(R.layout.banner_grid_item, null);
            vgPoints.addView(view);
        }
        vpPager.setCurrentItem(1);
        setViewPoint();
        // pagerAdapter.setData(banners);
        pagerAdapter.notifyDataSetChanged();
        startScroll();
    }

    private void setViewPoint() {
        for (int i = 0; i < vgPoints.getChildCount(); i++) {
            View view = vgPoints.getChildAt(i);
            ImageView img = (ImageView) view.findViewById(R.id.banner_grid_item_image);

            if (i == vpPager.getCurrentItem() - 1) {
                img.setBackgroundResource(R.drawable.banner_grid_cur);
            } else {
                img.setBackgroundResource(R.drawable.banner_grid);
            }
        }
    }

    /**
     * Banner点击事件
     */
    private class BannerClick implements BannerOnClickListener {
        @Override
        public void click(BannerInfo info) {
            clickBanner(info);
        }
    }

    /**
     * Banner的滚动事件
     */
    private class PagerChangeListener implements OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int i) {
            int pageIndex = i;
            if (i == 0) {
                // 当视图在第一个时，将页面号设置为图片的最后一张。
                pageIndex = banners.size() - 2;
            } else if (i == banners.size() - 1) {
                // 当视图在最后一个是,将页面号设置为图片的第一张。
                pageIndex = 1;
            }
            if (i != pageIndex) {
                vpPager.setCurrentItem(pageIndex, false);
            }
            setViewPoint();
            startScroll();

        }
    }

    Runnable action = new Runnable() {
        @Override
        public void run() {
            int currentItem = vpPager.getCurrentItem();
            currentItem++;
            if (currentItem >= pagerAdapter.getCount()) {
                currentItem = 0;
            }
            vpPager.setCurrentItem(currentItem);
        }
    };

    private void startScroll() {
        if (banners.size() <= 1) {
            return;
        }
        vpPager.removeCallbacks(action);
        vpPager.postDelayed(action, DefaultConsts.BANNER_CHANGE_TIME);
    }

    @Override
    public void dispose() {
        isLastSuccess = false;
        if (null != banners) {
            banners.removeAll(banners);
        }
        if (null != data) {
            data.removeAll(data);
        }
        super.dispose();
        if (null != vpPager && null != action) {
            vpPager.removeCallbacks(action);
        }
    }

    /**
     * 获取banner的listen
     */
    private class GetBannerAction extends BaseAction {
        @Override
        public void OK(final Object obj) {
            // banners = (List<BannerInfo>) obj;
            List<BannerInfo> temp = (List<BannerInfo>) obj;
            banners.clear();
            if (temp.size() > 1) {
                banners.add(temp.get(temp.size() - 1));
                banners.addAll(temp);
                banners.add(temp.get(0));
            } else {
                banners = temp;
            }

            //			System.out.println("精品页面banner obj:" + banners.toString());
            bActivity.getUIHandler().post(new Runnable() {
                @Override
                public void run() {
                    // pagerAdapter = new ViewPagerAdapter(bActivity, views, new
                    // BannerClick());
                    // vpPager.setAdapter(pagerAdapter);
                    pagerAdapter.setData(banners);
                    initBannerView();
                }
            });
            bannerAction = null;
        }

        @Override
        protected void onError() {
            super.onError();
            bannerAction = null;
        }
    }

    /**
     * @param flag 0:加载中，1：加载完成，-1：加载失败
     */
    private void updateLoadingView(int flag) {
        switch (flag) {
            case -1:
                loadingView.setVisibility(View.VISIBLE);
                freshView.setVisibility(View.GONE);
                if (animationDrawable != null && animationDrawable.isRunning()) {
                    animationDrawable.stop();
                }
                loadingImg.setBackgroundResource(R.drawable.home_loading_fail);
                break;
            case 0:
                loadingView.setVisibility(View.VISIBLE);
                freshView.setVisibility(View.GONE);
                loadingImg.setBackgroundResource(R.drawable.home_progress);
                animationDrawable = (AnimationDrawable) loadingImg.getBackground();
                animationDrawable.start();
                break;
            case 1:
                loadingView.setVisibility(View.GONE);
                freshView.setVisibility(View.VISIBLE);
                break;
        }
    }

    private GuideDialog dialog;
    private boolean showHelp;
    private QyreaderPerferencesUtil util;

    private void initGuide() {
        util = QyreaderPerferencesUtil.getInstance(bActivity, DefaultConsts.FIRST_TAG);
        dialog = new GuideDialog(bActivity, R.layout.about_service_dialog);
    }

    private void showGuide() {
        showHelp = util.isFirstBoutiqueLogin();
        if (showHelp) {
            if (null != dialog && !dialog.isShowing()) {
                //简单粗暴 解决偶现弹框崩溃的问题
                try {
                    dialog.show();
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            }
            util.setFirstBoutiqueLogin(false);
        }
    }

    public class BookListHandler extends ObjectHttpResponseHandler<List<ColumnInfos>> {

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            // TODO Auto-generated method stub
            bActivity.getUIHandler().post(new Runnable() {
                @Override
                public void run() {
                    updateLoadingView(-1);
                }
            });
            isLastSuccess = false;
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, List<ColumnInfos> object, String responseString) {
            Log.i(responseString);
            // TODO Auto-generated method stub
            if (object != null) {
                data = object;
                isLastSuccess = true;
                bActivity.getUIHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                        lv.post(new Runnable() {
                            @Override
                            public void run() {
                                freshView.scrollTo(0, 0);
                            }
                        });
                    }
                });
            }
            NewActionHelp.qryBookSortList(context, new GetSortListHandler());
            NewActionHelp.qryGameList(context, 0, new GetGameListHandler());
        }

        @Override
        public Type getType() {
            // TODO Auto-generated method stub
            return new TypeToken<List<ColumnInfos>>() {
            }.getType();

        }

    }

    /************************************
     * 分类start
     *****************************/
    private List<BookColumnInfos> columns = new ArrayList<>();

    private class FLAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return columns.size();
        }

        @Override
        public Object getItem(int arg0) {
            return columns.get(arg0);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            FLViewHolder holder;
            BookColumnInfos c = (BookColumnInfos) getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(bActivity).inflate(R.layout.boutique_fl_item, null);
                holder = new FLViewHolder();
                holder.rlFlHead = (RelativeLayout) convertView.findViewById(R.id.boutique_fl_head);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.boutique_fl_item_img);
                holder.tv = (TextView) convertView.findViewById(R.id.boutique_fl_item_title);
                holder.gv = (TeamInfoGridView) convertView.findViewById(R.id.boutique_fl_item_gv);
                convertView.setTag(holder);
            } else holder = (FLViewHolder) convertView.getTag();
            if (Utils.isNotEmpty(c.getBackColor())) {
                holder.rlFlHead.setBackgroundColor(Color.parseColor(c.getBackColor()));
            }
            //	ImageUtil.loadWebUrl(c.getPicUrl(), holder.ivImg);
            ImageLoaderUtil.loadWebUrl(c.getPicUrl(), holder.ivImg, ImageLoaderUtil.getLowOptions(R.drawable.boutique_fl_img));
            holder.tv.setText(c.getName());
            FLGridAdapter gvAdapter = new FLGridAdapter();
            gvAdapter.setData(c.getSubSortList());
            holder.gv.setAdapter(gvAdapter);
            holder.gv.setFocusable(false);
            holder.gv.setFocusableInTouchMode(false);
            return convertView;
        }
    }

    class FLGridAdapter extends BaseAdapter {
        List<BookColumnInfos> items = new ArrayList<>();

        public void setData(List<BookColumnInfos> is) {
            this.items = is;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final BookColumnInfos i = (BookColumnInfos) getItem(position);
            convertView = LayoutInflater.from(bActivity).inflate(R.layout.boutique_fl_grid_item, null);
            TextView tvName = (TextView) convertView.findViewById(R.id.boutique_fl_grid_item_tv);
            tvName.setText(i.getName());
            tvName.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Bundle b = new Bundle();
                    b.putInt("firstSortId", i.getParentId());
                    b.putInt("secondSortId", i.getSortId());
                    b.putString("columnName", i.getName());
                    b.putInt("mark", 2);
                    bActivity.goColumnDetailActivity(b);
                }
            });
            return convertView;
        }
    }

    class FLViewHolder {
        private RelativeLayout rlFlHead;
        ImageView ivImg;
        TextView tv;
        TeamInfoGridView gv;
    }

    public class GetSortListHandler extends ObjectHttpResponseHandler<List<BookColumnInfos>> {

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            // TODO Auto-generated method stub
            Log.i(responseString);
            isLastSuccess = false;
            bActivity.getUIHandler().post(new Runnable() {
                @Override
                public void run() {
                    updateLoadingView(-1);
                }
            });
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, List<BookColumnInfos> object, String responseString) {
            // TODO Auto-generated method stub
            Log.i(responseString);
            isLastSuccess = true;
            if (object != null) {
                columns.clear();
                columns.addAll(object);
                bActivity.getUIHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        flAdapter.notifyDataSetChanged();
                        lvFL.post(new Runnable() {
                            @Override
                            public void run() {
                                freshView.scrollTo(0, 0);
                            }
                        });
                    }
                });
            }
            headView.setVisibility(View.VISIBLE);
            updateLoadingView(1);
        }

        @Override
        public Type getType() {
            // TODO Auto-generated method stub
            return new TypeToken<List<BookColumnInfos>>() {
            }.getType();

        }
    }

    /************************************* 分类end *********************************/
    /*************************************
     * 游戏start
     *********************************/

    private List<GameColumnInfos> gameColumns = new ArrayList<GameColumnInfos>();

    private class GameAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return gameColumns.size();
        }

        @Override
        public Object getItem(int arg0) {
            return gameColumns.get(arg0);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            GameViewHolder holder;
            final GameColumnInfos c = (GameColumnInfos) getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(bActivity).inflate(R.layout.boutique_game_item, null);
                holder = new GameViewHolder();
                holder.llMore = (LinearLayout) convertView.findViewById(R.id.boutique_item_more_layout);
                holder.tv = (TextView) convertView.findViewById(R.id.boutique_item_title);
                holder.lv = (ListView) convertView.findViewById(R.id.boutique_game_item_listview);
                convertView.setTag(holder);
            } else {
                holder = (GameViewHolder) convertView.getTag();
            }
            holder.tv.setText(c.getColumnName());
            GameListAdapter gameListAdapter = new GameListAdapter();
            gameListAdapter.setData(c.getGameList(), c.getPhoneIndexShowNum());
            holder.lv.setAdapter(gameListAdapter);
            Utils.setListViewHeightBasedOnChildren(holder.lv, gameListAdapter);
            holder.lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent webIntent = new Intent(bActivity, GameWebActivity.class);
                    webIntent.putExtra(DefaultConsts.title_s, c.getGameList().get(position).getGameName());
                    webIntent.putExtra(DefaultConsts.req_url_s, c.getGameList().get(position).getUrl());
                    //webIntent.putExtra(DefaultConsts.req_url_s, "http://201591614.fangshui123.com/game/games/ljsp/");
                    bActivity.startActivity(webIntent);
                }
            });
            holder.llMore.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(bActivity, ColumnGameMoreActivity.class);
                    intent.putExtra("columnId", c.getColumnId());
                    intent.putExtra("columnName", c.getColumnName());
                    bActivity.startActivity(intent);
                }
            });
            return convertView;
        }
    }

    private class GameListAdapter extends BaseAdapter {

        private List<GameInfos> gameList = new ArrayList<>();
        private int count;

        public void setData(List<GameInfos> games, int count) {
            this.gameList = games;
            this.count = count;
        }

        @Override
        public int getCount() {
            return count < gameList.size() ? count : gameList.size();
        }

        @Override
        public Object getItem(int position) {
            return gameList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(bActivity).inflate(R.layout.boutique_game_list_item, null);
            ImageView gameImg = (ImageView) convertView.findViewById(R.id.boutique_game_img);
            TextView gameName = (TextView) convertView.findViewById(R.id.boutique_game_name);
            TextView gameIntro = (TextView) convertView.findViewById(R.id.boutique_game_intro);

            GameInfos game = (GameInfos) getItem(position);

            ImageLoaderUtil.loadWebUrl(game.getPicUrl(), gameImg, ImageLoaderUtil.getLowOptions(R.drawable.boutique_fl_img));
            gameName.setText(game.getGameName());
            gameIntro.setText(game.getIntro());

            return convertView;
        }

    }

    class GameViewHolder {
        private LinearLayout llMore;
        TextView tv;
        ListView lv;
    }

    public class GetGameListHandler extends ObjectHttpResponseHandler<List<GameColumnInfos>> {
        @Override
        public Type getType() {
            return new TypeToken<List<GameColumnInfos>>() {
            }.getType();
        }

        @Override
        public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {

        }

        @Override
        public void onSuccess(int arg0, Header[] arg1, List<GameColumnInfos> arg2, String arg3) {
            isLastSuccess = true;
            if (arg2 != null) {
                gameColumns.clear();
                gameColumns.addAll(arg2);
                //				System.err.println("game" + gameColumns.toString() + "arg3" + arg3);
                bActivity.getUIHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        gameAdapter.notifyDataSetChanged();
                        lvGame.post(new Runnable() {
                            @Override
                            public void run() {
                                freshView.scrollTo(0, 0);
                            }
                        });
                    }
                });
            }
            headView.setVisibility(View.VISIBLE);
            updateLoadingView(1);

        }
    }
    /************************************ 游戏end *********************************/
}
